/**
 * Created by zhangxiaoyu on 16/12/10.
 */
angular.module('hyt.myWaybillService', ['ngResource'])

  .factory('MyWaybills', function($resource, $rootScope,ENV,$state,$ionicLoading) {
    var ApiUrl = ENV.postUrl+ENV.api.getWaybills,
      waybillData={},pageSize=10;

    var resource = $resource(ApiUrl+':waybillSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getWaybillData : function(waybillSearch){
        var hasNextPage = true;
        resource.query({
          waybillSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            parameter:waybillSearch.parameter,
            userMsg:waybillSearch.userMsg
          })
        },function(r){
          console.log(r);
          if(r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            waybillData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('WaybillList.Updated');
        })
      },
      getResult:function(){
        if(waybillData[pageSize]===undefined){
          return false;
        }
        return waybillData[pageSize].data;
      },
      getMoreWaybillData : function(waybillSearch){
        if(waybillData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = waybillData[pageSize].hasNextPage;
        var nextPage = waybillData[pageSize].nextPage;
        var moreWaybillData = waybillData[pageSize].data;
        resource.query({
          waybillSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            parameter:waybillSearch.parameter,
            userMsg:waybillSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreWaybillData = moreWaybillData.concat(r.rows);

          waybillData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreWaybillData
          };
          $rootScope.$broadcast('WaybillList.Updated');
        })
      },
      hasNextPage: function() {
        if (waybillData[pageSize] === undefined) {
          return false;
        }
        return waybillData[pageSize].hasNextPage;
      }
    }
  })
  .factory('WaybillDetail',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.getWaybillDetail,
      result;

    var resource = $resource(ApiUrl+':waybillDetail',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getWaybillDetail : function(waybillDetail){
        return resource.query({
          waybillDetail:waybillDetail
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('WaybillDetail.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('WaybillGoods',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.getGoods,
      result;

    var resource = $resource(ApiUrl+':waybillGoods',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getWaybillGoods : function(waybillGoods){
        return resource.query({
          waybillGoods:waybillGoods
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('WaybillGoods.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('DownLoadAbnormalImg',function($resource, $rootScope,ENV){

    var ApiUrl = ENV.postUrl+ENV.api.downLoadAbnormalPicture,
      result;

    var resource = $resource(ApiUrl+':downLoadImg',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getDownLoadImg : function(downLoadImg){
        return resource.query({
          downLoadImg:downLoadImg
        },function(r){
          console.log(r);
          result=r;
          $rootScope.$broadcast('DownLoadAbnormalImg.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('DownLoadRectiptImg',function($resource, $rootScope,ENV){

    var ApiUrl = ENV.postUrl+ENV.api.downLoadRectiptPocture,
      result;

    var resource = $resource(ApiUrl+':downLoadImg',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getDownLoadImg : function(downLoadImg){
        return resource.query({
          downLoadImg:downLoadImg
        },function(r){
          console.log(r);
          result=r;
          $rootScope.$broadcast('DownLoadRectiptImg.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('WaybillRecord',function($resource, $rootScope,ENV,$ionicLoading,$state){

    var ApiUrl = ENV.postUrl+ENV.api.getWaybillRecord,
      result;

    var resource = $resource(ApiUrl+':waybillRecord',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getRecord : function(waybillRecord){
        return resource.query({
          waybillRecord:waybillRecord
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('WaybillRecord.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
